# -*- coding: utf8 -*-

from google.appengine.ext import db
from google.appengine.ext import webapp
from book.models import *
from book import utils
from book import dao
import logging

class ShopPage(webapp.RequestHandler):
    u"""店铺维护"""
    
    def get(self):
        u"""进入店铺维护画面"""
        shops = Shop.all()
        context = {
                   'shops' : shops,
                   'current_module' : 'shop'
        }

        utils.render_to_response(self.response, "shop.html", context)
        
    def post(self):
        u"""创建店铺"""
        name = self.request.get("name")
        telephone = self.request.get("telephone")
        shop = dao.create_shop(name,telephone);
        if shop:
            logging.getLogger().debug(u'创建店铺： %s' % shop);
            utils.render_to_success(self.response, u'创建店铺成功')
        else:
            logging.getLogger().debug(u'创建店铺失败: %s, %s' % (name, telephone))
            utils.render_to_error(self.response, 'shop_e001', u'创建店铺失败')
        
    def delete(self, key):
        u"""删除店铺"""
        logging.getLogger().debug(u'删除店铺： %s' % key);
        shop = Shop.get(db.Key(key))
        if shop:
            dishes = Dish.gql('WHERE shop = :1', shop).fetch(1000)
            for dish in dishes:
                orders = Order.gql('WHERE dish = :1', dish).fetch(1000)
                for order in orders:
                    order.delete()
                dish.delete()
            shop.delete();  
            utils.render_to_success(self.response, u'删除店铺成功')
        else:    
            logging.getLogger().debug(u'*************************fail delete shop')
            utils.render_to_error(self.response,'shop_e002', u'删除店铺失败')
        
        
class ShopListPage(webapp.RequestHandler):
    def get(self):
        shops = Shop.all()
        context = {
            'shops' : shops,
        }

        utils.render_to_response(self.response, "shoplist.xml", context)
        
class DishListPage(webapp.RequestHandler):
    def get(self, shop_key):
        shop = Shop.get(db.Key(shop_key))
        if shop:
            dishes = Dish.gql('WHERE shop = :1', shop).fetch(200)
        else:
            dishes = []
            
        context = {
            'dishes' : dishes
        }
        
        utils.render_to_response(self.response,"dishlist.html", context)  
         
          
                    
class DishPage(webapp.RequestHandler):
    def post(self, shop_key):
        try:
            shop = Shop.get(db.Key(shop_key))
            name = self.request.get('name')
            price = float(self.request.get('price'))
            dish = Dish(shop = shop, name = name, price = price)
            dish.put()
                    
            utils.render_to_success(self.response,u'菜单添加成功')  
        except Exception, e:
            utils.render_to_error(self.response,'dish_e001', e) 

    def delete(self, shop_key, dish_key):
        logging.getLogger().debug(dish_key)
        try:
            shop = Shop.get(db.Key(shop_key))
            dish = Dish.get(db.Key(dish_key))
            if shop and dish:
                orders = Order.gql('WHERE dish = :1', dish)
                for order in orders:
                    order.delete()
                dish.delete()
                                    
            utils.render_to_success(self.response,u'菜单删除成功')  
        except Exception, e:
            utils.render_to_error(self.response,'dish_e002', e) 
                    
             